#define PCRE2_CODE_UNIT_WIDTH 8
#include <stdio.h>
#include <string.h>
#include <pcre2.h>

int main(int argc, char **argv)
{
    PCRE2_SIZE erroroffset; PCRE2_SIZE *ovector; PCRE2_SIZE subject_length;
    PCRE2_SPTR pattern; PCRE2_SPTR subject; PCRE2_SPTR name_table;
    int crlf_is_newline; int errornumber; int rc; int utf8;
    pcre2_code *re;
    pcre2_match_data *match_data;
    uint32_t option_bits; uint32_t namecount; uint32_t name_entry_size; uint32_t newline;

    if (argc - 1 != 2) { printf("Exactly two arguments required: a regex and a subject string\n"); return 1; }
    pattern = (PCRE2_SPTR)argv[1];
    subject = (PCRE2_SPTR)argv[2];
    subject_length = (PCRE2_SIZE)strlen((char *)subject);
    re = pcre2_compile(
            pattern,               /* the pattern */
            PCRE2_ZERO_TERMINATED, /* indicates pattern is zero-terminated */
            0,                     /* default options */
            &errornumber,          /* for error number */
            &erroroffset,          /* for error offset */
            NULL);                 /* use default compile context */

    if (re == NULL) {
        PCRE2_UCHAR buffer[256];
        pcre2_get_error_message(errornumber, buffer, sizeof(buffer));
        printf("PCRE2 compilation failed at offset %d: %s\n", (int)erroroffset, buffer);
        return 1;
    }

    match_data = pcre2_match_data_create_from_pattern(re, NULL);

    rc = pcre2_match( re, subject, subject_length,
            0,                    /* start at offset 0 in the subject */
            0,                    /* default options */
            match_data,           /* block for storing the result */
            NULL);                /* use default match context */
    if (rc < 0) {
        switch(rc) {
            case PCRE2_ERROR_NOMATCH: printf("No match\n"); break; /* Handle other special cases if you like */
            default: printf("Matching error %d\n", rc); break;
        }
        pcre2_match_data_free(match_data);   /* Release memory used for the match */
        pcre2_code_free(re);                 /*   data and the compiled pattern. */
        return 1;
    }

    ovector = pcre2_get_ovector_pointer(match_data); printf("Match succeeded at offset %d\n", (int)ovector[0]);
    pcre2_match_data_free(match_data);  /* Release the memory that was used */
    pcre2_code_free(re);                /* for the match data and the pattern. */
    return 0;                           /* Exit the program. */
}
